﻿

//

using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.Extensions.Options;
using LaserMark.Components.Events;
using LaserMark.Extensions;
using LaserMark.Models;
using LaserMark.Components.Services;
using LaserMark.Components.Utils;
using LaserMark.Control.Service;
using LaserMark.View.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;

namespace LaserMark.Components;
public class Monitor : ComponentBase
{
    [Parameter]
    [NotNull]
    public RenderFragment? ChildContent { get; set; }

    [Inject]
    [NotNull]
    private IControl? Control { get; set; }

    [Inject]
    [NotNull]
    private IOptions<MarkOption>? MarkOption { get; set; }

    [Inject]
    [NotNull]
    private IGlobalStatus? GlobalStatus { get; set; }

    [NotNull]
    private CancellationTokenSource? CancellationToken { get; set; } = new();

    [NotNull]
    private System.Timers.Timer? Timer { get; set; }

    [NotNull]
    private System.Timers.Timer? TimeoutTimer { get; set; }

    [Inject]
    [NotNull]
    private IRibbonTabEventBus? EventBus { get; set; }

    [NotNull]
    private MarkOption? Options { get; set; }

    [Inject]
    private ToastService? ToastService { get; set; }

    private bool IsStop { get; set; }

    private bool IsAuto { get; set; }

    private bool IsManual { get; set; }

    private bool IsRuning { get; set; }

    private bool IsRun { get; set; }

    private bool IsReset { get; set; }

    private System.Timers.Timer? TimeOutTimer { get; set; }

    private CancellationTokenSource? CancellationTokenSource { get; set; } = new();

    private CancellationTokenSource? StopToken { get; set; } = new();

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Options = MarkOption.Value;

    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {

            Timer = new System.Timers.Timer(100);
            Timer.Elapsed += Timer_Elapsed;
            Timer.Start();
        }
    }

    private async void Timer_Elapsed(object? sender, ElapsedEventArgs e)
    {
        try
        {
            await Task.Delay(100);
            if (GlobalStatus.IsRunning)
            {
                int value;
                await Control.GetDi(0, mc.MC_GPI, out value);
                var status = Convert.ToString(value, 2).PadLeft(16, '0');
                // 急停逻辑
                if (status[13] == '1')
                {
                    IsRuning = false;
                    if (!IsStop)
                    {
                        IsStop = true;
                        mc.GT_SetDoBit(0, mc.MC_GPO, 1, 1);
                        mc.GT_SetDoBit(0, mc.MC_GPO, 2, 0);
                        mc.GT_SetDoBit(0, mc.MC_GPO, 3, 1);
                        mc.GT_SetDoBit(0, mc.MC_GPO, 4, 1);
                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                        await Control.SetDoBit(0, mc.MC_GPO, 9, 1);
                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                        await Control.SetDoBit(0, mc.MC_GPO, 11, 1);
                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                        await Control.Stop(0, 1 << 8, 1 << 8);
                        await Control.Stop(0, 1 << (2 - 1), 1 << (2 - 1));
                        await Control.Stop(0, 1 << (3 - 1), 1 << (3 - 1));
                        await Control.Stop(0, 1 << (4 - 1), 1 << (4 - 1));
                        IsRun = false;
                        CancellationTokenSource?.Cancel();
                        GlobalStatus.StopToken!.Cancel();
                        await Control.AxisOff(0, 2);
                        await Control.AxisOff(0, 3);
                        await Control.AxisOff(0, 4);
                        GlobalStatus.IsEmergencyStop = true;
                        GlobalStatus.IsXZeroBack = false;
                        GlobalStatus.IsYZeroBack = false;
                        GlobalStatus.IsZZeroBack = false;
                        await EventBus.PublishAsync(new RibbonTabEventMessage(new BootstrapBlazor.Components.RibbonTabItem() { Command = RibbonTabConstants.Stop }));
                    }
                }
                else
                {
                    IsStop = false;
                    if (!IsRuning)
                    {
                        IsRuning = true;
                        if (GlobalStatus.IsEmergencyStop)
                        {
                            await Control.ClearAxisStatus(0, 2, 1);
                            await Control.ClearAxisStatus(0, 3, 1);
                            await Control.ClearAxisStatus(0, 4, 1);
                            await Control.AxisOn(0, 2);
                            await Control.AxisOn(0, 3);
                            await Control.AxisOn(0, 4);
                            mc.GT_SetDoBit(0, mc.MC_GPO, 1, 0);
                        }
                        GlobalStatus.IsEmergencyStop = false;
                        await Control.SetDoBit(0, mc.MC_GPO, 2, 1);
                    }
                }

                if (status[12] == '0' && !IsAuto)
                {
                    IsAuto = true;
                    IsManual = false;
                    await Control.SetSoftLimit(0, (short)Options.AxisCode.X, (int)(Options.SoftMaxLimit.X / Options.XPluse.Value), (int)(Options.SoftMinLimit.X / Options.XPluse.Value));
                    await Control.SetSoftLimit(0, (short)Options.AxisCode.Y, (int)(Options.SoftMaxLimit.Y / Options.YPluse.Value), (int)(Options.SoftMinLimit.Y / Options.YPluse.Value));
                    await Control.SetSoftLimit(0, (short)Options.AxisCode.Z, (int)(Options.SoftMaxLimit.Z / Options.ZPluse.Value), (int)(Options.SoftMinLimit.Z / Options.ZPluse.Value));
                }
                else if (status[12] == '1' && !IsManual)
                {
                    IsManual = true;
                    IsAuto = false;
                    await Control.SetSoftLimit(0, (short)Options.AxisCode.X, int.MaxValue, int.MinValue);
                    await Control.SetSoftLimit(0, (short)Options.AxisCode.Y, int.MaxValue, int.MinValue);
                    await Control.SetSoftLimit(0, (short)Options.AxisCode.Z, int.MaxValue, int.MinValue);
                }


                // 设置软限位
                if (status[1] == '0' && status[12] == '1' && GlobalStatus.IsOnlyTransfor)
                {
                    if (status[4] == '0' && !IsRun && !GlobalStatus.IsEmergencyStop)
                    {
                        IsRun = true;
                        await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                        await Task.Delay(Options.ComprehensiveSetting.TransUpDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                        await Task.Delay(Options.ComprehensiveSetting.OnStageStartDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                        string status3;

                        do
                        {
                            await Task.Delay(1);
                            int value6;
                            await Control.GetDi(0, mc.MC_GPI, out value6);
                            status3 = Convert.ToString(value6, 2).PadLeft(16);
                        } while (status3[4] == '0');

                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                        if (GlobalStatus.IsOnlyTransfor)
                        {
                            await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                        }
                        IsRun = false;
                    }
                }

                if (status[1] == '0' && status[12] == '0')
                {
                    if (!GlobalStatus.IsMarked && status[4] == '0' && !IsRun && !GlobalStatus.IsEmergencyStop)
                    {
                        IsRun = true;
                        await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                        await Task.Delay(Options.ComprehensiveSetting.TransUpDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                        await Task.Delay(Options.ComprehensiveSetting.OnStageStartDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                        string status3;

                        do
                        {
                            await Task.Delay(1);
                            int value6;
                            await Control.GetDi(0, mc.MC_GPI, out value6);
                            status3 = Convert.ToString(value6, 2).PadLeft(16);
                        } while (status3[4] == '0');

                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                        GlobalStatus.IsTransComplete = true;
                        IsRun = false;
                    }
                }


                // 自动 或 仅过渡
                await Control.GetDi(0, mc.MC_GPI, out int valueac);
                status = Convert.ToString(valueac, 2).PadLeft(16, '0');
                if (!GlobalStatus.IsMarked && GlobalStatus.IsZZeroBack && GlobalStatus.IsXZeroBack && GlobalStatus.IsYZeroBack)
                {
                    if (status[0] == '1' && !IsRun && !GlobalStatus.IsEmergencyStop && status[4] == '1' && (status[12] == '0' || GlobalStatus.IsOnlyTransfor))
                    {
                        IsRun = true;
                        await Task.Delay(Options.ComprehensiveSetting.OutPieceWaitDelay);
                        int value1;
                        await Control.GetDi(0, mc.MC_GPI, out value1);
                        var status1 = Convert.ToString(value1, 2).PadLeft(16, '0');
                        if (status1[0] == '1')
                        {
                            CancellationTokenSource = new CancellationTokenSource();
                            int value2;
                            await Control.GetDi(0, mc.MC_GPI, out value2);
                            var status2 = Convert.ToString(value2, 2).PadLeft(16);
                            GlobalStatus.IsStateTimeOut = false;
                            await Control.SetDoBit(0, mc.MC_GPO, 6, 1);
                            await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                            if (!IsReset)
                            {
                                await Task.Delay(Options.ComprehensiveSetting.TransUpDelay);
                                IsReset = true;
                            }

                            await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                            if (Options.ComprehensiveSetting.IsOnStageStartDelay)
                            {
                                await Task.Delay(Options.ComprehensiveSetting.OnStageStartDelay);
                                await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                            }
                            else
                            {
                                string status3;
                                do
                                {
                                    await Task.Delay(1);
                                    int value6;
                                    await Control.GetDi(0, mc.MC_GPI, out value6);
                                    status3 = Convert.ToString(value6, 2).PadLeft(16);
                                } while (status3[4] == '1' && !CancellationTokenSource.IsCancellationRequested);
                                if (!CancellationTokenSource.IsCancellationRequested)
                                {
                                    if (status3[4] == '0')
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                    }
                                }
                                else
                                {
                                    await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                    await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                    await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                    IsRun = false;
                                    IsReset = false;
                                }
                            }


                            // 台面没有玻璃
                            if (status2[4] == '0')
                            {
                                if (status2[12] == '1')
                                {
                                    if (!GlobalStatus.IsOnlyTransfor)
                                    {
                                        int time = 1;
                                        TimeoutTimer = new(1000);
                                        TimeoutTimer.Elapsed += (sender, args) =>
                                        {
                                            Interlocked.Increment(ref time);
                                        };
                                        TimeoutTimer.Start();

                                        string status4;
                                        do
                                        {
                                            await Task.Delay(100);
                                            int value3;
                                            await Control.GetDi(0, mc.MC_GPI, out value3);
                                            status4 = Convert.ToString(value3, 2).PadLeft(16);
                                        } while (status4[4] == '0' && time <= Options.ComprehensiveSetting.OnStageStartTimeout && !CancellationTokenSource.IsCancellationRequested);

                                        if (CancellationTokenSource.IsCancellationRequested)
                                        {
                                            TimeoutTimer.Close();
                                            TimeoutTimer.Dispose();
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                            IsReset = false;
                                        }
                                        else
                                        {
                                            if (status4[4] == '1')
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();
                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);

                                                if (status4[12] == '1')
                                                {
                                                    if (GlobalStatus.IsOnlyTransfor)
                                                    {
                                                        await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                                    }
                                                }
                                                IsRun = false;
                                                IsReset = false;
                                                GlobalStatus.IsTransComplete = true;
                                            }

                                            if (time > Options.ComprehensiveSetting.OnStageStartTimeout)
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();
                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                                GlobalStatus.IsStateTimeOut = true;
                                                GlobalStatus.IsTransComplete = false;
                                                IsRun = false;
                                                IsReset = false;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (status[1] == '0')
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                            string status3;

                                            do
                                            {
                                                await Task.Delay(1);
                                                int value6;
                                                await Control.GetDi(0, mc.MC_GPI, out value6);
                                                status3 = Convert.ToString(value6, 2).PadLeft(16);
                                            } while (status3[4] == '0');

                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                            GlobalStatus.IsTransComplete = true;
                                            IsRun = false;
                                        }
                                        else
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                        }
                                    }
                                }
                                else
                                {
                                    if (status[1] == '0')
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                        string status3;

                                        do
                                        {
                                            await Task.Delay(1);
                                            int value6;
                                            await Control.GetDi(0, mc.MC_GPI, out value6);
                                            status3 = Convert.ToString(value6, 2).PadLeft(16);
                                        } while (status3[4] == '0');

                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        GlobalStatus.IsTransComplete = true;
                                        IsRun = false;
                                    }
                                    else
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        IsRun = false;
                                    }
                                }
                            }
                            else
                            {
                                // 台面有玻璃
                                string status5;
                                do
                                {
                                    await Task.Delay(1);
                                    int value4;
                                    await Control.GetDi(0, mc.MC_GPI, out value4);
                                    status5 = Convert.ToString(value4, 2).PadLeft(16);
                                } while (status5[4] == '1' && !CancellationTokenSource.IsCancellationRequested);


                                await Task.Delay(2000);
                                await Control.SetDoBit(0, mc.MC_GPO, 11, 1);

                                if (status5[12] == '1')
                                {
                                    if (!GlobalStatus.IsOnlyTransfor)
                                    {
                                        int time = 1;
                                        TimeoutTimer = new(1000);
                                        TimeoutTimer.Elapsed += (sender, args) =>
                                        {
                                            Interlocked.Increment(ref time);
                                        };
                                        TimeoutTimer.Start();

                                        string status6;
                                        do
                                        {
                                            await Task.Delay(1);
                                            int value5;
                                            await Control.GetDi(0, mc.MC_GPI, out value5);
                                            status6 = Convert.ToString(value5, 2).PadLeft(16);
                                        } while (status6[4] == '0' && time <= Options.ComprehensiveSetting.OnStageStartTimeout && !CancellationTokenSource.IsCancellationRequested);


                                        if (CancellationTokenSource.IsCancellationRequested)
                                        {
                                            TimeoutTimer.Close();
                                            TimeoutTimer.Dispose();
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                        }
                                        else
                                        {
                                            if (status6[4] == '1')
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();
                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                                IsRun = false;

                                                if (status6[12] == '1')
                                                {
                                                    if (GlobalStatus.IsOnlyTransfor)
                                                    {
                                                        await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                                    }
                                                }
                                                IsReset = false;
                                                GlobalStatus.IsTransComplete = true;
                                            }

                                            if (time > Options.ComprehensiveSetting.OnStageStartTimeout)
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();

                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                                GlobalStatus.IsStateTimeOut = true;
                                                IsRun = false;
                                                IsReset = false;
                                                GlobalStatus.IsTransComplete = false;
                                            }
                                        }
                                    }
                                    else
                                    {

                                        if (status5[1] == '1' && status[4] == '1')
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                        }
                                        else
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                            string status3;

                                            do
                                            {
                                                await Task.Delay(1);
                                                int value6;
                                                await Control.GetDi(0, mc.MC_GPI, out value6);
                                                status3 = Convert.ToString(value6, 2).PadLeft(16);
                                            } while (status3[4] == '0');

                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            if (GlobalStatus.IsOnlyTransfor)
                                            {
                                                await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                            }
                                            GlobalStatus.IsTransComplete = true;
                                            IsRun = false;
                                        }
                                    }
                                }
                                else
                                {
                                    if (status5[1] == '1')
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        IsRun = false;
                                    }
                                    else
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                        string status3;

                                        do
                                        {
                                            await Task.Delay(1);
                                            int value6;
                                            await Control.GetDi(0, mc.MC_GPI, out value6);
                                            status3 = Convert.ToString(value6, 2).PadLeft(16);
                                        } while (status3[4] == '0');

                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        if (GlobalStatus.IsOnlyTransfor)
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                        }
                                        GlobalStatus.IsTransComplete = true;
                                        IsRun = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            IsRun = false;
                        }
                    }
                }

                // 手动模式
                if (!GlobalStatus.IsMarked && GlobalStatus.IsZZeroBack && GlobalStatus.IsXZeroBack && GlobalStatus.IsYZeroBack)
                {
                    if (status[0] == '1' && !IsRun && !GlobalStatus.IsEmergencyStop && status[12] == '1' && !GlobalStatus.IsOnlyTransfor)
                    {
                        IsRun = true;
                        await Task.Delay(Options.ComprehensiveSetting.OutPieceWaitDelay);
                        int value1;
                        await Control.GetDi(0, mc.MC_GPI, out value1);
                        var status1 = Convert.ToString(value1, 2).PadLeft(16, '0');
                        if (status1[0] == '1')
                        {
                            CancellationTokenSource = new CancellationTokenSource();
                            int value2;
                            await Control.GetDi(0, mc.MC_GPI, out value2);
                            var status2 = Convert.ToString(value2, 2).PadLeft(16);
                            GlobalStatus.IsStateTimeOut = false;
                            await Control.SetDoBit(0, mc.MC_GPO, 6, 1);
                            await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                            if (!IsReset)
                            {
                                await Task.Delay(Options.ComprehensiveSetting.TransUpDelay);
                                IsReset = true;
                            }

                            await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                            if (Options.ComprehensiveSetting.IsOnStageStartDelay)
                            {
                                await Task.Delay(Options.ComprehensiveSetting.OnStageStartDelay);
                                await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                            }
                            else
                            {
                                string status3;
                                do
                                {
                                    await Task.Delay(1);
                                    int value6;
                                    await Control.GetDi(0, mc.MC_GPI, out value6);
                                    status3 = Convert.ToString(value6, 2).PadLeft(16);
                                } while (status3[4] == '1' && !CancellationTokenSource.IsCancellationRequested);
                                if (!CancellationTokenSource.IsCancellationRequested)
                                {
                                    if (status3[4] == '0')
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                    }
                                }
                                else
                                {
                                    await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                    await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                    await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                    IsRun = false;
                                    IsReset = false;
                                }
                            }

                            if (status2[4] == '0')
                            {
                                if (status2[12] == '1')
                                {
                                    if (!GlobalStatus.IsOnlyTransfor)
                                    {
                                        int time = 1;
                                        TimeoutTimer = new(1000);
                                        TimeoutTimer.Elapsed += (sender, args) =>
                                        {
                                            Interlocked.Increment(ref time);
                                        };
                                        TimeoutTimer.Start();

                                        string status4;
                                        do
                                        {
                                            await Task.Delay(100);
                                            int value3;
                                            await Control.GetDi(0, mc.MC_GPI, out value3);
                                            status4 = Convert.ToString(value3, 2).PadLeft(16);
                                        } while (status4[4] == '0' && time <= Options.ComprehensiveSetting.OnStageStartTimeout && !CancellationTokenSource.IsCancellationRequested);

                                        if (CancellationTokenSource.IsCancellationRequested)
                                        {
                                            TimeoutTimer.Close();
                                            TimeoutTimer.Dispose();
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                            IsReset = false;
                                        }
                                        else
                                        {
                                            if (status4[4] == '1')
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();
                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);

                                                if (status4[12] == '1')
                                                {
                                                    if (GlobalStatus.IsOnlyTransfor)
                                                    {
                                                        await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                                    }
                                                }
                                                IsRun = false;
                                                IsReset = false;
                                                GlobalStatus.IsTransComplete = true;
                                            }

                                            if (time > Options.ComprehensiveSetting.OnStageStartTimeout)
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();
                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                                GlobalStatus.IsStateTimeOut = true;
                                                GlobalStatus.IsTransComplete = false;
                                                IsRun = false;
                                                IsReset = false;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (status[1] == '0')
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                            string status3;

                                            do
                                            {
                                                await Task.Delay(1);
                                                int value6;
                                                await Control.GetDi(0, mc.MC_GPI, out value6);
                                                status3 = Convert.ToString(value6, 2).PadLeft(16);
                                            } while (status3[4] == '0');

                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                            GlobalStatus.IsTransComplete = true;
                                            IsRun = false;
                                        }
                                        else
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                        }
                                    }
                                }
                                else
                                {
                                    if (status[1] == '0')
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                        string status3;

                                        do
                                        {
                                            await Task.Delay(1);
                                            int value6;
                                            await Control.GetDi(0, mc.MC_GPI, out value6);
                                            status3 = Convert.ToString(value6, 2).PadLeft(16);
                                        } while (status3[4] == '0');

                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        GlobalStatus.IsTransComplete = true;
                                        IsRun = false;
                                    }
                                    else
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        IsRun = false;
                                    }
                                }
                            }
                            else
                            {
                                string status5;
                                do
                                {
                                    await Task.Delay(1);
                                    int value4;
                                    await Control.GetDi(0, mc.MC_GPI, out value4);
                                    status5 = Convert.ToString(value4, 2).PadLeft(16);
                                } while (status5[4] == '1' && !CancellationTokenSource.IsCancellationRequested);


                                await Task.Delay(2000);
                                await Control.SetDoBit(0, mc.MC_GPO, 11, 1);

                                if (status5[12] == '1')
                                {
                                    if (!GlobalStatus.IsOnlyTransfor)
                                    {
                                        int time = 1;
                                        TimeoutTimer = new(1000);
                                        TimeoutTimer.Elapsed += (sender, args) =>
                                        {
                                            Interlocked.Increment(ref time);
                                        };
                                        TimeoutTimer.Start();

                                        string status6;
                                        do
                                        {
                                            await Task.Delay(1);
                                            int value5;
                                            await Control.GetDi(0, mc.MC_GPI, out value5);
                                            status6 = Convert.ToString(value5, 2).PadLeft(16);
                                        } while (status6[4] == '0' && time <= Options.ComprehensiveSetting.OnStageStartTimeout && !CancellationTokenSource.IsCancellationRequested);


                                        if (CancellationTokenSource.IsCancellationRequested)
                                        {
                                            TimeoutTimer.Close();
                                            TimeoutTimer.Dispose();
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                        }
                                        else
                                        {
                                            if (status6[4] == '1')
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();
                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                                IsRun = false;

                                                if (status6[12] == '1')
                                                {
                                                    if (GlobalStatus.IsOnlyTransfor)
                                                    {
                                                        await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                                    }
                                                }
                                                IsReset = false;
                                                GlobalStatus.IsTransComplete = true;
                                            }

                                            if (time > Options.ComprehensiveSetting.OnStageStartTimeout)
                                            {
                                                TimeoutTimer.Close();
                                                TimeoutTimer.Dispose();

                                                await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);

                                                await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                                await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                                await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                                GlobalStatus.IsStateTimeOut = true;
                                                IsRun = false;
                                                IsReset = false;
                                                GlobalStatus.IsTransComplete = false;
                                            }
                                        }
                                    }
                                    else
                                    {

                                        if (status5[1] == '1' && status[4] == '1')
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            IsRun = false;
                                        }
                                        else
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                            string status3;

                                            do
                                            {
                                                await Task.Delay(1);
                                                int value6;
                                                await Control.GetDi(0, mc.MC_GPI, out value6);
                                                status3 = Convert.ToString(value6, 2).PadLeft(16);
                                            } while (status3[4] == '0');

                                            await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                            await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                            await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                            if (GlobalStatus.IsOnlyTransfor)
                                            {
                                                await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                            }
                                            GlobalStatus.IsTransComplete = true;
                                            IsRun = false;
                                        }
                                    }
                                }
                                else
                                {
                                    if (status5[1] == '1')
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        IsRun = false;
                                    }
                                    else
                                    {
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 0);
                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 0);
                                        string status3;

                                        do
                                        {
                                            await Task.Delay(1);
                                            int value6;
                                            await Control.GetDi(0, mc.MC_GPI, out value6);
                                            status3 = Convert.ToString(value6, 2).PadLeft(16);
                                        } while (status3[4] == '0');

                                        await Control.SetDoBit(0, mc.MC_GPO, 12, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransStopDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 8, 1);
                                        await Task.Delay(Options.ComprehensiveSetting.TransDownDelay);
                                        await Control.SetDoBit(0, mc.MC_GPO, 10, 1);
                                        if (GlobalStatus.IsOnlyTransfor)
                                        {
                                            await Control.SetDoBit(0, mc.MC_GPO, 11, 0);
                                        }
                                        GlobalStatus.IsTransComplete = true;
                                        IsRun = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            IsRun = false;
                        }
                    }
                }

            }
        }
        catch (Exception)
        {
        }
    }
}
